#! /usr/bin/env python
# -*- coding: utf-8 -*-
import time
import hashlib
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from utils.redis_obj.RedisObj import Client
from conf import settings
__HOST = 'localhost'
__PORT = 8080

tsocket = TSocket.TSocket(__HOST, __PORT)
transport = TTransport.TBufferedTransport(tsocket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
conn = Client(protocol)
transport.open()

def auth_check():
    """
    和API验证
    :return:
    """
    ctime = time.time()
    key = settings.APIKEY
    dynamic_key = "{0}|{1}".format(key, ctime)
    m = hashlib.md5()
    m.update(bytes(dynamic_key, encoding='utf-8'))
    md5_key = m.hexdigest()
    md5_dynamic_key = "{0}|{1}".format(md5_key, ctime)

    return md5_dynamic_key

keys = conn.keys(auth_check(),'*')
for key in keys:
    key_value =None
    key_type = conn.type(auth_check(),key)
    print(key,key_type)
    if key_type == 'list':
        len_ = conn.llen(auth_check(),key)
        key_value = conn.lrange(auth_check(),key,0,len_)
    elif key_type == 'set':
        key_value = conn.smembers(auth_check(),key)
    elif key_type == 'string':
        key_value = conn.get(auth_check(),key)
    elif key_type == 'hash':
        hash_keys = conn.hkeys(auth_check(),key)
        key_value = conn.hvals(auth_check(),key)

    print('value',key_value)
